package com.example.server.controller;

import com.example.server.bean.Admin;
import com.example.server.bean.AdminLoginParam;
import com.example.server.util.RespBean;
import com.example.server.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * ProjectName: yeb
 * Author: 别怿言
 * Date: 2023-02-04 0:31
 */
@Api(tags = "LoginController")
@RestController
public class Logincontroller {

    @Autowired
    private IAdminService adminService;

    @ApiOperation(value = "登陆成功返回token")
    @PostMapping("/login")
    public RespBean Login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest request){
        return adminService.login(adminLoginParam.getUsername(),adminLoginParam.getPassword(),
                adminLoginParam.getCode(),request);
    }

    @ApiOperation(value = "获取登录用户信息")
    @PostMapping("/admin/info")
    public Admin getAdminInfo(Principal principal){
        if(null==principal){
            return null;
        }
        String username = principal.getName();
        Admin admin = adminService.getAdminByUserName(username);
        admin.setPassword(null);
        admin.setRoles(adminService.getRolesByAdminId(admin.getId()));
        return admin;
    }

    @ApiOperation(value = "退出登陆")
    @PostMapping("/logout")
    public RespBean Logout(){
        return RespBean.success("注销成功");
    }
}
